package com.wenwei.paper.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wenwei.paper.R;
import com.wenwei.paper.adapter.ReplenishmentNotifyAdapter;
import com.wenwei.paper.base.AtyContainer;
import com.wenwei.paper.base.BaseActivity;
import com.wenwei.paper.base.Constant;
import com.wenwei.paper.bean.PageBean;
import com.wenwei.paper.bean.ReplenishmentEndListBean;
import com.wenwei.paper.bean.ReplenishmentListBean;
import com.wenwei.paper.net.ApiManager;
import com.wenwei.paper.net.BaseSubscriber;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/13.
 */
public class ReplenishmentNotifyActivity extends BaseActivity {
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.recycler)
    XRecyclerView recycler;

    private ReplenishmentNotifyAdapter mAdapter;
    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_replenishment_notify;
    }

    @Override
    protected void initView() {
        super.initView();
        baseTitle.setText("补货通知");
        recycler.setLayoutManager(new LinearLayoutManager(mContext));
        recycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                recycler.setLoadingMoreEnabled(true);
                getReplenishmentList(1);
            }

            @Override
            public void onLoadMore() {
                if (currentPage < pageTotalCount) {
                    currentPage = currentPage + 1;
                    recycler.setLoadingMoreEnabled(true);
                    getReplenishmentList(2);
                } else {
                    recycler.setLoadingMoreEnabled(false);
                    recycler.loadMoreComplete();
                }
            }
        });
        mAdapter = new ReplenishmentNotifyAdapter(new ReplenishmentNotifyAdapter.CallBack() {
            @Override
            public void onItemClick(int id) {
                Bundle bundle = new Bundle();
                bundle.putInt("id",id);
                goActivity(mContext,ReplenishmentDetailsActivity.class,bundle);
            }
        });
        recycler.setAdapter(mAdapter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.base_back)
    public void onViewClicked() {
        exit();
    }

    @Override
    protected void initData() {
        super.initData();
        getReplenishmentList(1);
    }

    private int currentPage = 1;//当前分页
    private int pageTotalCount;//分页总数
    ArrayList<ReplenishmentListBean> mList = new ArrayList<>();
    private BaseSubscriber<PageBean<ArrayList<ReplenishmentListBean>>> replenishmentSubscriber;
    private void getReplenishmentList(final int type){
        replenishmentSubscriber = new BaseSubscriber<PageBean<ArrayList<ReplenishmentListBean>>>(mContext) {
            @Override
            public void onSuccess(PageBean<ArrayList<ReplenishmentListBean>> replenishmentListBeen) {

                pageTotalCount = replenishmentListBeen.getLast_page();

                if(type == Constant.LOAD_TYPE_REFRESH){
                    mList.clear();
                    mList.addAll(replenishmentListBeen.getData());
                    mAdapter.setReplenishmentData(mList);
                    recycler.refreshComplete();
                }else{
                    mList.addAll(replenishmentListBeen.getData());
                    mAdapter.setReplenishmentData(mList);
                    recycler.loadMoreComplete();
                }
                mAdapter.notifyDataSetChanged();
            }
        };
        ApiManager.getInstance().getReplenishmentList(replenishmentSubscriber,currentPage);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            exit();
            return false;
        }else {
            return super.onKeyDown(keyCode, event);
        }

    }

    private void exit(){
        if(AtyContainer.activityStack.size()<=1){
            goActivity(mContext,HostActivity.class);
            finish();
        }else {
            finish();
        }
    }
}
